package com.leetcode.hot100;

import java.util.Arrays;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode.hot100
 * @Author: lei
 * @CreateTime: 2024-03-25  13:59
 * @Description: TODO
 * @Version: 1.0
 */
public class 合并两个有序数组 {
    public static void main(String[] args) {
        int[] nums1 = {0};
        int[] nums2 = {1};
        int m = 0, n = 1;
        merge(nums1, m, nums2, n);
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int indexM = m - 1, indexN = n - 1, k = m + n - 1, cur;
        while (indexN >= 0 || indexM >= 0) {
            if (indexM == -1) {
                cur = nums2[indexN--];
            } else if (indexN == -1) {
                cur = nums1[indexM--];
            } else if (nums1[indexM] > nums2[indexN]) {
                cur = nums1[indexM--];
            } else {
                cur = nums2[indexN--];
            }
            nums1[k--] = cur;
        }

        System.out.println(Arrays.toString(nums1));

    }
}

